package com.my.msgtype;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.util.ArrayList;
import java.util.List;

//发送批量消息
public class Producer4 {
    public static void main(String[] args) throws MQClientException, RemotingException, InterruptedException, MQBrokerException {
        //1.谁来发(rocketmq客户端，设置生产者组)
        DefaultMQProducer producer = new DefaultMQProducer("group1");
        //2.发给谁(本机的namespace server，namseserver负责发给broker)
        producer.setNamesrvAddr("localhost:9876");
        producer.start();//连接到nameserver
        //3.怎么发
        //4.发什么

        List<Message> msgList = new ArrayList<>();
        msgList.add(new Message("topic1","tag1", "abc".getBytes()));
        msgList.add(new Message("topic1","tag1", "bcd".getBytes()));
        msgList.add(new Message("topic1","tag1", "cef".getBytes()));

        SendResult sendResult = producer.send(msgList);//发送延时
        System.out.println(sendResult);
        System.out.println("发送完成");
        //6.打扫战场
        producer.shutdown();//关闭生产者连接
    }
}
